﻿/* 
 * Memories Version 1.0
 * Developed by Jigar Desai (http://www.jigar.net/) 
 */
using System;
using System.Configuration;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;

namespace Memories
{
	/// <summary>
	/// AlbumSettingsConfigSectionHandler
	/// </summary>
	public class AlbumSettingsConfigSectionHandler : IConfigurationSectionHandler
	{
		public object Create(object parent, object configContext, System.Xml.XmlNode section)
		{
			// Create an instance of XmlSerializer based on the RewriterConfiguration type...
			XmlSerializer ser = new XmlSerializer(typeof(SiteSettings));

			// Return the Deserialized object from the Web.config XML
			return ser.Deserialize(new XmlNodeReader(section));
		}

        public static Settings GetSettings(string hostHeader){
            if (HttpContext.Current.Cache["SiteSettings"] == null)
            {
                //SiteSettings settings = (SiteSettings)ConfigurationSettings.GetConfig("SiteSettings");
                SiteSettings settings = (SiteSettings)ConfigurationManager.GetSection("SiteSettings");


                foreach (Settings setting in settings.SettingsCollection)
                {
                    foreach (PropertyExtractorPlugin plugin in setting.PropertyExtractorPlugins)
                    {
                        plugin.PropertyPlugin = Activator.CreateInstance(Type.GetType(plugin.PluginName)) as IPropertyPlugin;
                    }
                }
                HttpContext.Current.Cache["SiteSettings"] = settings;
            }

            return ((SiteSettings)HttpContext.Current.Cache["SiteSettings"]).SettingsCollection[hostHeader];

        }
	}
	
	
	/// <summary>
	/// Settings for WebApplication.
	/// </summary>
	[Serializable()]
	public class SiteSettings
	{
       
        public SettingsCollection SettingsCollection ;

	}
	
	/// <summary>
	/// Collection of Settings.
	/// </summary>
	[Serializable()]
	public class SettingsCollection : CollectionBase 
	{

		public virtual void Add(Settings s) 
		{
			this.InnerList.Add(s);
		}

		public Settings this[int index] 
		{
			get { return (Settings)this.InnerList[index]; }
			set { this.InnerList[index] = value; }
		}

		public Settings this[string index] 
		{

			get 
			{
				foreach (Settings sett in this.InnerList) 
				{
					if (sett.HostHeader.ToLower() == index.ToLower()) 
					{
						return sett;
					}
				}
				return null;
			}
		}

	}

	/// <summary>
	/// Setting for Single WebApplication.
	/// </summary>
	[Serializable()]
	public class Settings
	{
		string _Title;
		string _AlbumPath;
		string _TempPath;
		string _CSSStylePath;
		string _HostHeader = "localhost";
		int _PageSize = 16;
		FileTypes _FileTypes = new FileTypes();
		string _DescriptionFile = "description.txt";
		
        IUrlFormatter _UrlFormatter = new SimpleUrlFormatter();
        string _UICulture = "en-US";
        PropertyExtractorPluginCollection _PropertyExtractorPlugins;

        /// <summary>
        /// Collection of Plugins for File Property.
        /// </summary>
        public PropertyExtractorPluginCollection PropertyExtractorPlugins { get { return _PropertyExtractorPlugins; } set { _PropertyExtractorPlugins = value; } }


        public string UICulture
        {
            get { return _UICulture; }
            set { _UICulture = value; }
        }


        

        public IUrlFormatter UrlFormatter
        {
            get { return _UrlFormatter; }
        }

		/// <summary>
		/// Title of application.
		/// </summary>
		public string Title { get { return _Title; } set { _Title = value; } }

		/// <summary>
		/// Physical path on file system which contains all albums.
		/// location have read permission. 
		/// </summary>
		public string AlbumPath { get { return _AlbumPath; } set { _AlbumPath = value; } }

		/// <summary>
		/// Physical path on file system where thumbnails and Images to be displayed 
		/// on web pages are to created, location must have write permission.
		/// </summary>
		public string TempPath { get { return _TempPath; } set { _TempPath = value; } }


		/// <summary>
		/// Relative path on web application where style sheet is located.
		/// </summary>
		public string CSSStylePath { get { return _CSSStylePath; } set { _CSSStylePath = value; } }

		/// <summary>
		/// Host header of album.
		/// </summary>
		public string HostHeader { get { return _HostHeader; } set { _HostHeader = value; } }

		/// <summary>
		/// No of Files to be displayed in single page.
		/// </summary>
		public int PageSize { get { return _PageSize; } set { _PageSize = value; } }

		/// <summary>
		/// Name of file that contains description of perticular folder
		/// or application.
		/// </summary>
		public string DescriptionFile { get { return _DescriptionFile; } set { _DescriptionFile = value; } }

		/// <summary>
		/// Collection of FileTypes.
		/// </summary>
		public FileTypes FileTypes { get { return _FileTypes; } set { _FileTypes = value; } }

	}

    [Serializable()]
    public class PathEncrypter
    {
        [XmlAttribute("type")]
        string type;
    }

    
	/// <summary>
	/// Description of FileType and information to process those files.
	/// </summary>
	[Serializable()]
	public class FileType
	{
		string _Extension = "";
		string _ContentType = "";
		string _IconImage = "";

		/// <summary>
		/// Extension of file (for example .jpg)
		/// </summary>
		[XmlAttribute("Extension")] 
		public string Extension { get { return _Extension; } set { _Extension = value; } }
        
		/// <summary>
		/// Content type of file (for jpg its image/jpeg)
		/// </summary>
		[XmlAttribute("ContentType")]
		public string ContentType { get { return _ContentType; } set { _ContentType = value; } }
        
		/// <summary>
		/// Icon's (image) relative path if it is to be used as thumbnail.
		/// </summary>
		[XmlAttribute("IconImage")]
		public string IconImage { get { return _IconImage; } set { _IconImage = value; } }
		public FileType() { }

		/// <summary>
		/// Constructor of FileType
		/// </summary>
		/// <param name="extension">Extension</param>
		/// <param name="contentType">CpntentType</param>
		/// <param name="iconImage">IconImage</param>
		public FileType(string extension, string contentType, string iconImage)
		{
			_Extension = extension;
			_ContentType = contentType;
			_IconImage = iconImage;
		}
	}
	
	/// <summary>
	/// Collection of File
	/// </summary>
	[Serializable()]
	public class FileTypes : CollectionBase
	{

		/// <summary>
		/// Adds new FileType to collection.
		/// </summary>
		/// <param name="f"></param>
		public virtual void Add(FileType f)
		{
			this.InnerList.Add(f);
		}

		/// <summary>
		/// Indexer
		/// </summary>
		public FileType this[int index]
		{
			get { return (FileType)this.InnerList[index]; }
			set { this.InnerList[index] = value; }
		}
		
		/// <summary>
		/// Indexer
		/// </summary>
		public FileType this[string index]
		{

			get
			{
				foreach (FileType filetype in this.InnerList)
				{
					if (filetype.Extension == index)
					{
						return filetype;
					}
				}
				return null;
			}
		}
		
		/// <summary>
		/// Checks of FileType is present in colleciton with given
		/// extension.
		/// </summary>
		/// <param name="extension">Extension of file</param>
		/// <returns></returns>
		public bool HasType(string extension)
		{
			foreach (FileType filetype in this.InnerList)
			{
				if (filetype.Extension.ToLower() == extension.ToLower())
				{
					return true;
				}
			}
			return false;
		}

	}

    /// <summary>
    /// PropertyExtractorPlugin
    /// </summary>
	[Serializable()]
	public class PropertyExtractorPlugin {
		[XmlAttribute("type")]
		public string PluginName;

        [XmlIgnore]
        public IPropertyPlugin PropertyPlugin;
	}

    /// <summary>
    /// Collection of PropertyExtractorPlugin
    /// </summary>
	[Serializable()]
	public class PropertyExtractorPluginCollection : CollectionBase
	{

		public virtual void Add(PropertyExtractorPlugin f)
		{
			this.InnerList.Add(f);
		}

		public PropertyExtractorPlugin this[int index]
		{
			get { return (PropertyExtractorPlugin)this.InnerList[index]; }
			set { this.InnerList[index] = value; }
		}
		
	}

}
